package ai.people.netmon.telegram.service.impl;

import ai.people.netmon.framework.domain.telegram.vo.attribution.Attribution;
import ai.people.netmon.framework.domain.telegram.vo.attribution.PhoneAttribution;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.attribution.AttributionRequest;
import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.businessenum.AnalysisTypeEnum;
import ai.people.netmon.telegram.mapper.AttributionMapper;
import ai.people.netmon.telegram.service.AccountAnalysisService;
import ai.people.netmon.telegram.service.AttributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 属地服务impl
 *
 * @author yuanqinglong
 * @date 2022/1/18 11:08
 */
@Service
public class AttributionServiceImpl implements AttributionService {


    @Autowired
    private AttributionMapper attributionMapper;

    @Autowired
    private AccountAnalysisService accountAnalysisServiceImpl;


    /**
     * 电话归属
     *
     * @param attributionRequest 页面关键字请求
     * @return {@link PageResponse}<{@link Attribution}>
     */
    @Override
    public PageResponse<PhoneAttribution> phoneAttributionList(AttributionRequest attributionRequest) {
        long attributionCount = attributionMapper.phoneAttributionCount(attributionRequest);
        if (attributionCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<PhoneAttribution> phoneAttributionList = attributionMapper.phoneAttributionList(attributionRequest);
        Set<String> analyzedAccountIdSet = accountAnalysisServiceImpl.analyzedAccountIdSet(new AnalyzedListRequest(attributionRequest.getUserId(), AnalysisTypeEnum.ACCOUNT.getCode(), AnalysisSourceEnum.telegram.name()));
        phoneAttributionList.forEach(phoneAttribution -> phoneAttribution.setJoinStatus(analyzedAccountIdSet.contains(String.valueOf(phoneAttribution.getId()))));
        return PageResponse.builder(phoneAttributionList, attributionCount);
    }



    /**
     * 地址定位
     *
     * @param attributionRequest 页面关键字请求
     * @return {@link PageResponse}<{@link PhoneAttribution}>
     */
    @Override
    public PageResponse<Attribution> addressTargetingList(AttributionRequest attributionRequest) {
        long targetingCount = attributionMapper.addressTargetingCount(attributionRequest);
        if (targetingCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<Attribution> targetingList = attributionMapper.addressTargetingList(attributionRequest);
        Set<String> analyzedAccountIdSet = accountAnalysisServiceImpl.analyzedAccountIdSet(new AnalyzedListRequest(attributionRequest.getUserId(), AnalysisTypeEnum.ACCOUNT.getCode(), AnalysisSourceEnum.telegram.name()));
        targetingList.forEach(phoneAttribution -> phoneAttribution.setJoinStatus(analyzedAccountIdSet.contains(String.valueOf(phoneAttribution.getId()))));
        return PageResponse.builder(targetingList, targetingCount);
    }
}
